<?php
final class Wenda
{
    static private $_registry = array();

    protected function __construct(){}

    private function __clone(){}

    public static function unRegister($key)
    {
        if (isset(self::$_registry[$key])) {
            if (is_object(self::$_registry[$key])) {
                self::$_registry[$key]->__destruct();
            }
            unset(self::$_registry[$key]);
        }
    }

    public static function setRegister($key, $value)
    {
        if (!isset(self::$_registry[$key])) {
            self::$_registry[$key] = $value;
        }
        return self::$_registry[$key];
    }

    public static function getRegister($key)
    {
        if (isset(self::$_registry[$key])) {
            return self::$_registry[$key];
        }
        return null;
    }

    public static function getNamespace()
    {
        $request = Zend_Controller_Front::getInstance()->getRequest();
        if (null !== $request) {
            $module = $request->getModuleName();
        }
        if (empty($module)) {
            $module = Zend_Controller_Front::getInstance()->getDefaultModule();
        }
        return ucfirst($module);
    }

    public static function getInflected($name)
    {
        $inflector = new Zend_Filter_Inflector(':class');
        $inflector->setRules(array(
            ':class'    => array('Word_CamelCaseToUnderscore')
        ));
        return ucfirst($inflector->filter(array('class'=>$name)));
    }

    public static function getModel($name , $module = null)
    {
        if (null === $module) {
            $module = self::getNamespace();
        }

        $key = self::getInflected($module . 'Model' . ucfirst($name));

        if (!self::getRegister($key)) {
            self::setRegister($key, new $key());
        }

        return self::getRegister($key);
    }

    public static function getService($name,$module = null)
    {
        if (null === $module) {
            $module = self::getNamespace();
        }

        $key = self::getInflected($module . 'Service' . ucfirst($name));

        if (!self::getRegister($key)) {
            self::setRegister($key, new $key());
        }

        return self::getRegister($key);
    }

    public static function getTable($name, $module = null)
    {
        if (null === $module) {
            $module = self::getNamespace();
        }

        $key = self::getInflected($module . 'Table' . ucfirst($name));
        if (!self::getRegister($key)) {
            self::setRegister($key, new $key());
        }
        return self::getRegister($key);
    }

    public static function getForm($name, $module = null)
    {
        if (null === $module) {
            $module = self::getNamespace();
        }

        $key = self::getInflected($module . 'Form' . ucfirst($name));
        if (!self::getRegister($key)) {
            self::setRegister($key, new $key());
        }
        return self::getRegister($key);
    }

    public static function isInstalled()
    {
        $key = 'installed';
        if (!self::getRegister($key)) {
            $config = new Zend_Config_Ini(APPLICATION_PATH . '/config/local.ini');
            if (null == $config->install || null == $config->install->date) {
                self::setRegister($key, false);
            } else {
                self::setRegister($key, true);
            }
        }
        return self::getRegister($key);
    }
}
?>
